package com.daka.service.impl;

import com.daka.dao.OrganizationDao;
import com.daka.entity.Member;
import com.daka.entity.MemberOrg;
import com.daka.entity.Organization;
import com.daka.enumerate.StatusCode;
import com.daka.service.MemberOrgService;
import com.daka.service.MemberService;
import com.daka.service.OrganizationService;
import com.daka.service.qnzyz.BHMember;
import com.daka.service.qnzyz.BHMemberService;
import com.daka.service.qnzyz.BHOrgService;
import com.daka.tools.*;
import com.daka.vo.response.OrganizationResponseVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangx
 * CreateTime: 2016/9/18 16:29
 */

@Service("organizitionService")
@Transactional
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationDao organizationDao;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberOrgService memberOrgService;
    @Autowired
    BHOrgService bhOrgService;
    @Autowired
    private BHMemberService bhMemberService;



    @Override
    public int insert(Organization po) {
        //检查当前是否已有相同名称的社团
        Organization organization1 = new Organization();
        organization1.setName(po.getName());
        List<Organization> list1 = organizationDao.selectByStatement(organization1);
        if(null != list1 && list1.size()>0){
            throw new ServiceRuntimeException(StatusCode.SERVER_EXCEPTION.getCode(),"中央团体注册失败");
        }

        Map<String, String> map = new HashMap<String, String>();
        map.put("groupName", po.getName());
        map.put("groupAdmCode", "");
        map.put("groupProvince", po.getProvince());
        map.put("groupCity", po.getCity());
        map.put("groupDistrict", po.getDistrict());
        map.put("groupAddress", po.getCity());
        map.put("groupPostcode", "");
        map.put("groupAdmMobile", "");
        map.put("groupAdmPwd", "");
        //创建者
        Member member = memberService.selectByPrimaryKey(po.getCreateUser());
        map.put("groupAdmName", member.getRealName());
        map.put("groupAdmIdNo", "");
        map.put("groupAdmTel", member.getMobile());
        map.put("groupCategory", "");
        map.put("groupSummary", "");
        try{
            String groupCode = bhOrgService.reg4Social(map);
            System.out.println("*********************************************"+groupCode);
            if(StringUtils.isNotBlank(groupCode)) {
                po.setGroupCode(groupCode);
            }else {
                throw new ServiceRuntimeException(StatusCode.SERVER_EXCEPTION.getCode(),"中央团体注册失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                throw e;
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }

        po.setCreateTime(DateUtil.getDate());
        po.setTimes(0);
        po.setMemberCounts(1);
        po.setMonitor(member.getRealName());
        po.setPhone(member.getMobile());

        organizationDao.insert(po);

        try {
            //志愿者一号通code
            String voluteerCode = member.getVolunteerCode();
            if (StringUtils.isBlank(member.getVolunteerCode()) || "null".equals(member.getVolunteerCode())) {
                voluteerCode = bhMemberService.simpleReg(BHMember.toBHMember(member, po.getGroupCode()));
                System.out.println("*********************************************"+voluteerCode);
                member.setVolunteerCode(voluteerCode);
                memberService.update(member);
            }
            //加入组织
            if (StringUtils.isNoneBlank(member.getVolunteerCode())) {
                MemberOrg memberOrg = new MemberOrg();
                memberOrg.setCreatetime(DateUtil.getDate());
                memberOrg.setStatus(DKQConstant.APPLY_STATUS_OK);
                memberOrg.setUserType(DKQConstant.ROLES_CREATEER);
                memberOrg.setMember(member.getId());
                memberOrg.setOrgid(po.getId());
                memberOrgService.insert(memberOrg);

                try {
                    //通知北航加入新创建的组织
                    bhMemberService.joinGroup(voluteerCode, po.getGroupCode());

                    String password = "";
                    Member memberP = memberService.selectByPrimaryKey(po.getCreateUser());
                    if (StringUtils.isNotBlank(memberP.getPassword())) {
                        password = memberP.getPassword();
                    } else {
                        password = MD5Util.MD5("123456");
                    }
                    bhOrgService.addManager(po.getGroupCode(), member.getRealName(), member.getMobile(), password, po.getName(), member.getCardNO(), po.getPhone());

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public int update(Organization po) {
        return organizationDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public Organization selectByPrimaryKey(Integer id) {
        return organizationDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Organization> selectByStatement(Organization po) {
        return organizationDao.selectByStatement(po);
    }

    @Override
    public Page<OrganizationResponseVo> selectByCity(String city, int pageNo){
        List<Organization> list = organizationDao.selectByCity(city,10,10*(pageNo-1));

        List<OrganizationResponseVo> listVos = new ArrayList<OrganizationResponseVo>();
        for(Organization aa:list){
            OrganizationResponseVo vo = OrganizationResponseVo.buildVO(aa);
            listVos.add(vo);
        }

        int count = organizationDao.selectByCityCount(city);
        Page<OrganizationResponseVo> page =  new Page<OrganizationResponseVo>();
        page.setList(listVos);
        page.setTotal(count);
        return page;
    }

    @Override
    public Organization selectByGroupCode(String groupCode){
        return organizationDao.selectByGroupCode(groupCode);
    }

}


